<template>
	<view>
		<field v-for="(field,index) in fieldList" :businessId="businessId" :key="field.id" :data="field" @fieldValueChange="fieldValueChange"/>
	</view>
</template>
<script>
import field from './field/index.vue'
export default {
    provide() {
        return {
            getFormInfo: ()=>{
                return {busId: this.businessId, formCode: this.formCode}
            }
        }
    },
    props: {
        fieldList: {
            type: Array,
            default: function(){return []}
        },
        businessId: {
            type: String,
            require: true
        },
        formCode: {
            type: String,
            require: true
        },
    },
    components: { field },
    methods:{
        fieldValueChange(obj){
            // console.log('======================================== fieldValueChange =========================================')
            let vm = this
            let form = {}
			console.log(obj, 99999);
			let idx = -1
            this.fieldList.forEach(function(f,index){
				let val = f.value
				if (f.id == obj.id) {
					idx = index
					val = obj.value
				}
                form[f.name] = val
            })
			if (idx > -1) this.fieldList[idx] = obj
			this.$emit('updateList', this.fieldList)
            function setFieldValue(name, value){
                let fields = vm.fieldList.filter(e=> e.name == name)
                fields.forEach(e=>{
                    e.value = value
                })
            }
            if (obj.changeFun) {
                try {
                    eval(obj.changeFun)
                } catch (err) {
                    console.error(err)
                }
            }
        }
    }
}
</script>